#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define time_s 365*24*60*60
#define min_out(x,y) x>y?y:x
 

void num(int *a)
{
    *a = 100;
}
void get_memory(int **p)
{
    *p = (int*)malloc(sizeof(int));
}

struct text
{
    char a;
    char b;
    char c;
 
   
};

typedef struct node
{
    char *name;
    struct node *next;
}Node;

typedef struct singly
{
    int num;
    struct singly *next;
}Singly;

typedef struct doubly
{
    char *name;
    struct doubly *next;
    struct doubly *prvious;
}Doubly;

Singly* Sort_B2S(Singly *head)
{
    Singly* current = head;
    Singly* previous = head;
    Singly* temp_head = head;
    Singly* temp_head_previous = head;
    Singly* head_out = head;

    for (int i = 0; i < 8; i++)
    {
        Singly *max = current;
        Singly *previous_max = current;
        while (current)
        {
            if (max->num < current->num)
            {
                max = current;
                previous_max = previous;
            }
            previous = current;
            current = current->next;
        }
        if (max == temp_head)
        {
            temp_head_previous = temp_head;
            temp_head = temp_head->next;
            current = temp_head;
        }
        else if (head_out->num < max->num)
        {
            previous_max->next = previous_max->next->next;
            max->next = head_out;
            head_out = max;
            temp_head_previous = max;
            current = temp_head;
        }
        else
        {     
            previous_max->next = previous_max->next->next;
            max->next = temp_head;
            temp_head_previous->next = max;
            temp_head_previous = max;
            current = temp_head;
        }
    }
    return head_out;
}

int main(int argc, char const *argv[])
{
    //printf("一年中的秒数为:%d\n",time_s);
    //printf("两个数中偏小的值是:%d\n",min_out(1,3));

    // int i = 3;
    // int a = i+++i+++i++;
    // printf("a的值为:%d\n",a);
    // printf("i的值为:%d\n",i);

    // int a = 10;
    // num(&a);
    // printf("num输出的值为:%d\n",a);

    // int *a;
    // get_memory(&a);
    // printf("a输出的值为:%d\n",*a);
    
   // struct text A;
    //printf("A的字节数为:%ld\n",sizeof(A));

    Node *i = (Node*)malloc(sizeof(Node));
    Node *j = (Node*)malloc(sizeof(Node));
    Node *o = (Node*)malloc(sizeof(Node));
    i->name = (char*)malloc(10*sizeof(char));
    j->name = (char*)malloc(10*sizeof(char));
    o->name = (char*)malloc(10*sizeof(char));
    strcpy(i->name,"i");
    strcpy(j->name,"j");
    strcpy(o->name,"o");
    i->next = j;
    j->next = o;
    printf("i的下一个节点是%s\n",i->next->name);
    i->next = i->next->next;
    if(j != NULL)
    {
        free(j);
        j=NULL;
    }
    printf("i的下一个节点是%s\n",i->next->name);
    printf("j储存的地址为%p\n",j);

    Doubly *x = (Doubly*)malloc(sizeof(Doubly));
    Doubly *y = (Doubly*)malloc(sizeof(Doubly));
    Doubly *z = (Doubly*)malloc(sizeof(Doubly));
    x->name = (char*)malloc(10*sizeof(char));
    y->name = (char*)malloc(10*sizeof(char));
    z->name = (char*)malloc(10*sizeof(char));
    strcpy(x->name,"x");
    strcpy(y->name,"y");
    strcpy(z->name,"z");
    x->next = y;
    y->prvious = x;
    printf("x的下一个节点是%s\n",x->next->name);
    printf("y的上一个节点是%s\n",y->prvious->name);
    y->next = z;
    z->prvious = y;
    printf("y的下一个节点是%s\n",y->next->name);
    printf("z的上一个节点是%s\n",z->prvious->name);

    Singly *a = (Singly*)malloc(sizeof(Singly));
    Singly *b = (Singly*)malloc(sizeof(Singly));
    Singly *c = (Singly*)malloc(sizeof(Singly));
    Singly *d = (Singly*)malloc(sizeof(Singly));
    
    Singly *e = (Singly*)malloc(sizeof(Singly));
    Singly *f = (Singly*)malloc(sizeof(Singly));
    Singly *g = (Singly*)malloc(sizeof(Singly));
    Singly *h = (Singly*)malloc(sizeof(Singly));
    a->num = 30;
    b->num = 40;
    c->num = 20;
    d->num = 25;
    e->num = 55;
    f->num = 33;
    g->num = 22;
    h->num = 77;
    a->next = b;
    b->next = c;
    c->next = d;
    d->next = e;
    e->next = f;
    f->next = g;
    g->next = h;
    Singly* new_head = Sort_B2S(a);
    Singly* temp_new_head = new_head;
    while (temp_new_head)
    {
        printf("当前值为%d\n",temp_new_head->num);
        temp_new_head = temp_new_head->next;
    }
    
    return 0;
}
